package com.aracky.system.service.impl;

import com.aracky.common.vo.PageVO;
import com.aracky.system.constants.ComConstants;
import com.aracky.system.mapper.SysRoleMenuMapper;
import com.aracky.system.mapper.SysUserRoleMapper;
import com.aracky.system.model.entity.SysRole;
import com.aracky.system.mapper.SysRoleMapper;
import com.aracky.system.model.entity.SysRoleMenu;
import com.aracky.system.model.entity.SysUserRole;
import com.aracky.system.model.request.SysRoleRequest;
import com.aracky.system.model.vo.SysRoleVO;
import com.aracky.system.model.vo.SysUserVO;
import com.aracky.system.service.SysRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author zzp
 * @since 2022-07-14 11:22:57
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;
    /**
     * 新增角色
     * @param sysRole
     * @return
     */
    @Override
    public int saveRole(SysRole sysRole) {
        return sysRoleMapper.insert(sysRole);
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @Override
    public int deleteRole(Long roleId) {
        return sysRoleMapper.deleteById(roleId);
    }

    /**
     * 角色修改
     * @param sysRole
     * @return
     */
    @Override
    public int updateRole(SysRole sysRole) {
        return sysRoleMapper.updateById(sysRole);
    }

    /**
     *查询角色详情
     * @param roleId
     * @return
     */
    @Override
    public SysRole getRoleById(Long roleId) {
        return sysRoleMapper.selectById(roleId);
    }

    /**
     * 查询角色列表
     * @param sysRoleRequest
     * @return
     */
    @Override
    public PageVO<SysRoleVO> getRolePage(SysRoleRequest sysRoleRequest) {
        IPage<SysRoleVO> page=sysRoleMapper.findRole(sysRoleRequest.toPage(),sysRoleRequest);
        return PageVO.convertPage(page);
    }

    /**
     * 分配用户给角色
     * @param roleId
     * @param userIds
     */
    @Override
    @Transactional
    public void saveUserToRole(Long roleId, Long[] userIds) {
        LambdaQueryWrapper<SysUserRole> lambdaQueryWrapper
                = Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getRoleId, roleId);
        sysUserRoleMapper.delete(lambdaQueryWrapper);

        if (userIds == null || userIds.length == 0) {
            return;
        }
        SysUserRole ur;
        // 新增用户与角色管理
        List<SysUserRole> list = new ArrayList<SysUserRole>(userIds.length);
        for (Long userId : userIds)
        {
            ur = new SysUserRole();
            ur.setUserId(userId);
            ur.setRoleId(roleId);
            list.add(ur);
        }
        sysUserRoleMapper.batchUserRole(list);
    }

    /**
     * 分配菜单给角色
     * @param roleId
     * @param menuIds
     */
    @Override
    @Transactional
    public void saveMenuToRole(Long roleId, Long[] menuIds) {
        LambdaQueryWrapper<SysRoleMenu> lambdaQueryWrapper
                = Wrappers.<SysRoleMenu>lambdaQuery().eq(SysRoleMenu::getRoleId, roleId);
        sysRoleMenuMapper.delete(lambdaQueryWrapper);

        if (menuIds == null || menuIds.length == 0) {
            return;
        }
        SysRoleMenu sysRoleMenu;
        // 新增菜单与角色管理
        List<SysRoleMenu> list = new ArrayList<SysRoleMenu>(menuIds.length);
        for (Long menuId : menuIds)
        {
            sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setRoleId(roleId);
            list.add(sysRoleMenu);
        }
        sysRoleMenuMapper.batchMenuRole(list);
    }

    /**
     * 检查角色名是否唯一
     * @param roleName
     * @return
     */
    @Override
    public Long getCheckRoleName(String roleName) {
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper
                = Wrappers.<SysRole>lambdaQuery().eq(SysRole::getRoleName, roleName);
        return sysRoleMapper.selectCount(lambdaQueryWrapper);
    }

    @Override
    public Long getDefaultRole(String roleKey) {
        //查询角色id
        LambdaQueryWrapper<SysRole> roleLambdaQueryWrapper =
                Wrappers.<SysRole>lambdaQuery().eq(SysRole::getRoleKey,roleKey);
        return  this.getOne(roleLambdaQueryWrapper).getRoleId();
    }
}
